﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SurveyHost.Models;

namespace SurveyHost.Controllers
{
    public class BaseController : Controller
    {
        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
        }
        protected override ViewResult View(IView view, object model)
        {
            ConfigureStandardViewDataValues(ViewData);
            return base.View(view, model);
        }
        protected override ViewResult View(string viewName, string masterName, object model)
        {
            ConfigureStandardViewDataValues(ViewData);
            return base.View(viewName, masterName, model);
        }
        protected virtual void ConfigureStandardViewDataValues(ViewDataDictionary viewData)
        {
            var c = Configuration.Retrieve();
            ViewData["WebsiteTitle"] = c.WebsiteTitle;
            ViewData["WebsiteByline"] = c.WebsiteByline;
        }


        /// <summary>
        /// Displays the error page to the user
        /// </summary>
        /// <remarks>This method does not set the standard ViewData values used by Site.Master.  It calls ErrorView(ErrorPresentation) which does.</remarks>
        /// <param name="title">The error title.</param>
        /// <param name="byline">The error byline.</param>
        /// <param name="message">The error message.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <param name="exception">The exception.</param>
        /// <returns><seealso cref="ActionResult"/></returns>
        protected virtual ActionResult ErrorView(string title, string byline, string message, string returnUrl, Exception exception)
        {
            return ErrorView(new ErrorPresentation { Title = title, Exception = exception, Byline = byline, Message = message, ReturnUrl = returnUrl });
        }

        /// <summary>
        /// Displays the error page to the user
        /// </summary>
        /// <param name="errorPresentation">The error presentation.</param>
        /// <returns><seealso cref="ActionResult"/></returns>
        private ActionResult ErrorView(ErrorPresentation errorPresentation)
        {
            ConfigureStandardViewDataValues(ViewData);
            return View("Error", errorPresentation);
        }
        /// <summary>
        /// Displays the success page to the user
        /// </summary>
        /// <remarks>This method does not set the standard ViewData values used by Site.Master.  It calls SuccessView(SuccessPresentation) which does.</remarks>
        /// <param name="title">The success title.</param>
        /// <param name="byline">The success byline.</param>
        /// <param name="message">The success message.</param>
        /// <param name="returnUrl">The return URL.</param>
        /// <returns><seealso cref="ActionResult"/></returns>
        protected virtual ActionResult SuccessView(string title, string byline, string message, string returnUrl)
        {
            return SuccessView(new SuccessPresentation { Title = title, Byline = byline, Message = message, ReturnUrl = returnUrl });
        }

        /// <summary>
        /// Displays the success page to the user
        /// </summary>
        /// <param name="successPresentation">The success presentation.</param>
        /// <returns><seealso cref="ActionResult"/></returns>
        private ActionResult SuccessView(SuccessPresentation successPresentation)
        {
            ConfigureStandardViewDataValues(ViewData);
            return View("Success", successPresentation);
        }
    }
}
